// pages/mine/mine.ts
var url = require("../../utils/urls");
var commons = require("../../utils/common");
var tip = require("../../utils/tip");
var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();
var lineChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      {
        title: '派单数',
        type: "numberOfOrders"
      },
      {
        title: '装车数',
        type: "loadingQuantity",
      },
      {
        title: '卸车数',
        type: "unloadingNumber",
      }
    ],
    tabIndex: 0,
    pageData: {},
    windowWidth: 320
  },

  touchHandler: function (e) {
    console.log(lineChart.getCurrentDataIndex(e));
    lineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },

  teamLeaderMyStatistics: async function (type) {
    let requestUrl = url.teamLeaderMyStatistics

    let data = await commons.postJson(requestUrl);
    if (data.code == 200) {
      let resData = data.data
      console.log(resData,'resData')
      this.setData({
        pageData: resData
      })

      let categories = []
      let yData = []
      resData.teamLeaderDataInfoRespVOS.forEach((item) => {
        let time = item['time'].substring(5, 7) + "." + item['time'].substring(8, 10)
        categories.push(time)
        yData.push(item[type])
      });
      let obj = {
        categories: categories,
        data: yData
      }
      this.dealStatistics(obj)
    } else {
      tip.showToast(data.msg, 'error', 1000)
    }
  },

  createSimulationData: function () {
    var categories = [];
    var data = [];
    for (var i = 0; i < 7; i++) {
      categories.push('6.' + (i + 1));
      data.push(Math.random() * (20 - 10) + 10);
    }
    return {
      categories: categories,
      data: data
    }
  },
  updateData: function (e) {
    this.setData({
      tabIndex: e.currentTarget.dataset.index
    })
    this.teamLeaderMyStatistics(this.data.list[this.data.tabIndex].type)
    // lineChart.updateData({
    //   categories: simulationData.categories,
    //   series: series
    // });
  },

  logout() {
    tip.pageDelayJump("/pages/login/login", null, 1000);
    wx.clearStorageSync()
  },

  dealStatistics(obj) {
    // var simulationData = this.createSimulationData();
    lineChart = new wxCharts({
      // color: ['#000000'],
      canvasId: 'lineCanvas',
      type: 'line',
      categories: obj.categories,
      animation: true,
      // background: '#f5f5f5',
      series: [
        {
          name: '数量',
          data: obj.data,
          format: function (val, name) {
            return val.toFixed(2);
          },
          // lineStyle: {
          //   color: '#000000' //改变折线颜色
          // }
        },
        // {
        //   name: '成交量2',
        //   data: [2, 0, 0, 3, null, 4, 0, 0, 2, 0],
        //   format: function (val, name) {
        //     return val.toFixed(2) + '万';
        //   }
        // }
      ],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        // title: '成交金额 (万元)',
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: this.data.windowWidth - 40,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        // lineStyle: 'curve'，
        lineColor: '#00000'
      },
      userInfo:{}
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {

    const userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo,'userInfo')
    this.setData({
      userInfo: userInfo
    })
    try {
      var res = wx.getSystemInfoSync();
      this.setData({
        windowWidth: res.windowWidth
      })
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    this.teamLeaderMyStatistics(this.data.list[this.data.tabIndex].type)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      tabIndex: 0,
    })
    this.teamLeaderMyStatistics(this.data.list[this.data.tabIndex].type)
    setTimeout(() => {
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})